package com.banciyuan.bcywebview.biz.ask.ask;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.myinterface.PushActionInterface;
import com.banciyuan.bcywebview.utils.http.CommonApiUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

public class WriteAskHelper {

    private Context mContext;
    private PushActionInterface mInterface;
    private RequestQueue mQueue;

    private Boolean ifAsking = false;

    private String ouid = "";
    private String mContent = "";

    public WriteAskHelper(Context mContext, PushActionInterface mInterface, String ouid) {
        this.mContext = mContext;
        this.mInterface = mInterface;
        this.ouid = ouid;
        mQueue = VolleyQueue.getRquest(this.mContext);
    }

    public void doPush(Boolean noName) {
        if (ifAsking) return;
        else ifAsking = true;

        Response.Listener<String> onSuccess = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext, mInterface)) {
                    mInterface.PushSuccess("");
                    ifAsking = false;
                } else {
                    ifAsking = false;
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mInterface.onNetError(mContext.getString(R.string.net_check_first));
                ifAsking = false;
            }
        };
        CommonApiUtils.doAsk(mContext, mQueue, onSuccess, onError, ouid,
                UserDataHelper.getInstance(mContext).getToken(), noName, mContent);
    }

    public void setContent(String mContent) {
        this.mContent = mContent;
    }
}